import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, UpdateResult } from 'typeorm';
import { WxUserOpenId } from './wxUserOpenId.entity';

@Injectable()
export class WxUserOpenIdService {
  constructor(
    @InjectRepository(WxUserOpenId)
    private repository: Repository<WxUserOpenId>,
  ) {}

  /**
   * 查询用户openid
   * @param appid 
   * @param unionid 
   * @returns 
   */
  findOpenId(appid: string, unionid: string): Promise<WxUserOpenId>{
    return this.repository.findOne({where:{
      appid:appid,
      unionid:unionid
    }})
  }

  /**
   * 创建一个用户关联
   * @param appid 
   * @param unionid 
   * @param openid 
   * @returns 
   */
  createOpenIdRela(appid: string, unionid: string, openid?: string): Promise<WxUserOpenId>{
    let wxUserOpenid = new WxUserOpenId()
    wxUserOpenid.appid = appid
    wxUserOpenid.unionid = unionid
    wxUserOpenid.openid = openid
    wxUserOpenid.create_time = new Date()
    return this.repository.save(wxUserOpenid)
  }
}
